package com.bdth.mixapi.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdth.mixapi.api.entity.*;
import com.bdth.mixapi.api.service.*;
import com.bdth.mixapi.api.vo.CommonList;
import com.bdth.mixapi.api.vo.CommonMessage;
import com.bdth.mixapi.data.CommonData;
import com.bdth.mixapi.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2019/4/12.
 */
@Configuration
public class ScheduledTask {

    @Autowired
    DeviceService deviceService;

    @Autowired
    PushmsgService pushmsgService;

    @Autowired
    RdOnlinerateService rdOnlinerateService;

    @Autowired
    MsglogService msglogService;

    @Autowired
    XjlogService xjlogService;

//    @Scheduled(cron = "0 36 14 ? * *")
//    public void job(){
//        System.out.println("开始："+System.currentTimeMillis());
//        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
//        String date = DateUtil.getLastDate(2);
//        queryWrapper.le("uptime",date);
//        boolean flag = deviceService.remove(queryWrapper);
//        System.out.println("结束："+System.currentTimeMillis());
//    }

    @Scheduled(cron = "0 36 05 ? * *")
    public void job(){
        System.out.println("开始："+System.currentTimeMillis());
        String date = DateUtil.format(new Date());
        createNowType(date);
        System.out.println("结束："+System.currentTimeMillis());
    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void pushmsg(){
        System.out.println("开始生产");
        Pushmsg pushmsg = new Pushmsg();
        pushmsg.setCode("20");
        String sj = DateUtil.getRandomDateTime("yyyy-MM-dd HH:mm:ss");
        Random ran=new Random();
        int sum = 20+ ran.nextInt(500);
        String msg = "["+sj+"]向专线平台推送"+sum+"条短报文数据。";
        pushmsg.setMsg(msg);
        pushmsgService.save(pushmsg);
        System.out.println("结束生产");
    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void pushWz(){
        System.out.println("Wz开始生产");
        Pushmsg pushmsg = new Pushmsg();
        pushmsg.setCode("30");
        String sj = DateUtil.getRandomDateTime("yyyy-MM-dd HH:mm:ss");
        Random ran=new Random();
        int sum = 200+ ran.nextInt(900);
        String msg = "["+sj+"]向位置监控平台推送"+sum+"条位置定位数据。";
        pushmsg.setMsg(msg);
        pushmsgService.save(pushmsg);
        System.out.println("Wz结束生产");
    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void pushJk(){
        System.out.println("Jk开始生产");
        Pushmsg pushmsg = new Pushmsg();
        pushmsg.setCode("40");
        String sj = DateUtil.getRandomDateTime("yyyy-MM-dd HH:mm:ss");
        Random ran=new Random();
        int sum = 30+ ran.nextInt(200);
        String msg = "["+sj+"]向健康平台推送"+sum+"条健康数据。";
        pushmsg.setMsg(msg);
        pushmsgService.save(pushmsg);
        System.out.println("Jk结束生产");
    }

    /**
     * 生产数据
     */
    @Scheduled(cron = "*/30 * * * * ?")
    public void msglogMessage(){
        System.out.println("开始生产");
        Msglog bean = new Msglog();
        bean.setCode("20");
        String sj = DateUtil.getRandomDateTime("yyyy-MM-dd HH:mm:ss");
        bean.setCrtime(sj);
        Random ran=new Random();
        int sum = 30+ ran.nextInt(200);
        Random ran1=new Random();
        int i = ran1.nextInt(29);
        if(CommonData.listRd==null){
            CommonData.listRd = getRd();
        }
        String msg = "";
        if(CommonData.listRd!=null) {
            msg = "[" + sj + "]向"+CommonData.listRd.get(i).getDepName()+ "推送" + sum + "条数据。";
        }
        bean.setMessage(msg);
        msglogService.save(bean);
        System.out.println("结束生产");

    }

    public void createNowType(String d){
        //查询有数据的最后一天
        QueryWrapper<Device> timequery = new QueryWrapper<>();
        timequery.orderByDesc("uptime");
        timequery.eq("type","3");
        timequery.last("limit 1");
        List<Device> list = deviceService.list(timequery);
        String now = null;
        if(list!=null&&list.size()>0) {
            now = DateUtil.format(list.get(0).getUptime());
        }
        QueryWrapper<Device> type3query = new QueryWrapper<>();
        type3query.eq("uptime",now);
        type3query.eq("type", "3");
        //最近一天的type3的数据
        List<Device> dl = deviceService.list(type3query);
        List<Device> result = new ArrayList<>();
        for(int i=0;i<dl.size();i++){
            Device dd = dl.get(i);
            dd.setUptime(DateUtil.toDate(d));
            result.add(dd);
        }
        //从type2中获取N条数据插入到dl中
        List<Device> tl2 = getType2(d);
        for(int i=0;i<tl2.size();i++){
            dl.add(tl2.get(i));
        }
        boolean f = deviceService.saveBatch(dl);
    }

    private List<Device> getType2(String d){
        //查询有数据的最后一天
        QueryWrapper<Device> timequery = new QueryWrapper<>();
        timequery.orderByDesc("uptime");
        timequery.eq("type","2");
        timequery.last("limit 1");
        List<Device> list = deviceService.list(timequery);
        String now = null;
        if(list!=null&&list.size()>0) {
            now = DateUtil.format(list.get(0).getUptime());
        }
        QueryWrapper<Device> type2query = new QueryWrapper<>();
        type2query.eq("type", "2");
        type2query.eq("uptime",now);
        //最近一天的type3的数据
        List<Device> dl = deviceService.list(type2query);
        List<Device> type3 = new ArrayList<>();
        Random rr = new Random();
        int size = 20+rr.nextInt(30);
        for(int i=0;i<size;i++){
            Random ran=new Random();
            int sum = ran.nextInt(dl.size());
            Device bean = new Device();
            Device b = dl.get(i);
            bean.setUptime(DateUtil.toDate(d));
            bean.setDevice(b.getDevice()+i);
            bean.setId(b.getId());
            bean.setType(3);
            Random r = new Random();
            double s = (1+ran.nextInt(9))*0.0002;
            bean.setLng(b.getLng().add(new BigDecimal(s)));
            bean.setLat(b.getLat().add(new BigDecimal(s)));
            type3.add(bean);
        }
        return type3;
    }

    private List<RdOnlinerate> getRd(){
        //查询有数据的最后一天
        QueryWrapper<RdOnlinerate> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<RdOnlinerate> list = rdOnlinerateService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<RdOnlinerate> queryWrapper = new QueryWrapper<>();

            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.orderByDesc("OnlineRate");
            queryWrapper.last("limit 30");
            List<RdOnlinerate> onlinerateList = rdOnlinerateService.list(queryWrapper);
            return onlinerateList;
        }else{
            return null;
        }
    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void xjmsg(){
        System.out.println("开始生产");
        Xjlog xjlog = new Xjlog();
        String sj = DateUtil.getRandomDateTime("HH:mm:ss");
        Random ran=new Random();
        int sum = 10+ ran.nextInt(100);
        String msg = sj+"<br>向综合平台推送"+sum+"条信息数据<br>";
        xjlog.setValue(msg);
        xjlogService.save(xjlog);
        System.out.println("结束生产");
    }
}
